<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  <style>
    .active {
      color: red;
    }
  </style>
</head>

<body>
  <div id="app">
    <ul>
      <router-link active-class="active" tag="li" to="/position/55">职位信息</router-link>
      <router-link active-class="active" tag="li" to="/search">搜索</router-link>
      <router-link active-class="active" tag="li" to="/my">我的</router-link>
    </ul>
    <div>
      <router-view></router-view>
    </div>

  </div>

  <script>


    var routes = [
      {
        path: "/position/:id",
        component: {
          props: ['id'],
          template: `
          <div>
            职位信息 {{id}}
          </div>`
        },
        props: true
      },
      {
        path: "/search", component: {
          props: ['username', 'age'],
          template: `
           <div>搜索{{username}}-{{age}}</div>
           `
        },
        props: {
          username: 'xiaoming', age: 20
        }

      },
      {
        path: "/login", component: { 
          template: `<div>登录页面</div>` ,
          mounted(){
            localStorage.setItem('islogin',true)
          }
        }
      }
    ]


    var router = new VueRouter({
      //两种路由模式：hash history
      mode: 'hash',
      routes
    })

    router.beforeEach((to, from, next) => {
      //可以在这做权限判断
      console.log(to)
      if(to.fullPath.indexOf('position')>-1 && !localStorage.getItem('islogin')){
        next({path:'/login'})
        return;
      }
      next();
    })


    var vm = new Vue({
      router
    }).$mount('#app')

  </script>

</body>

</html>